package com.vgemv.jsutilitysdk.Display.JsViewDisplayManager;

import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;


import java.util.HashMap;

/**
 * 仅仅负责:
 * 1,subview添加到super Viewgroup上,以及添加时的动画效果.
 * 2,subview从superview上删除,以及动画效果.
 * 3,如果super 就是一个view的话,添加不了,添加到这个view的parent上面去.
 */
public class JsViewDisplayManager {

    private static JsViewDisplayManager _sharedInstance;

    //主线程的Handler
    private static final Handler mHandler_main = new Handler(Looper.getMainLooper());


    JsViewDisplayManager() {

    }

    public static JsViewDisplayManager sharedInstance() {
        if(_sharedInstance == null) {
            _sharedInstance = new JsViewDisplayManager();
        }
        return _sharedInstance;
    }

    //用来保存contentView和其显示的style
     public  HashMap<View, JsViewDisplayStyle> map_style = new HashMap<>();

    /**
     * @param subview
     * @param superview
     * @param style
     * @param callback
     */
    public void display(final View subview, ViewGroup superview, JsViewDisplayStyle style, JsViewDisplayCallback callback) {

        //1 存储style
        if(style == null) {
            style = new JsViewDisplayStyle();
        }
        //contentView和style关联起来,并存储起来方便使用.
        map_style.put(subview,style);

        //2 maskView添加contentView.
        if(style.mLayoutParams_subviewInitialLayoutParams != null) {
            superview.addView(subview,style.mLayoutParams_subviewInitialLayoutParams);
        }
        else {
            superview.addView(subview);
        }

        switch(style.displayAnimationType) {
            case None:{
                break;
            }
            case FromBottom:{
                /**
                 * 在实际位置的基础上,应用该转换.
                 */
                final Animation translateAnimation = new TranslateAnimation(0,0,0,0,Animation.RELATIVE_TO_SELF,1, 0,0);
                //动画时间,毫秒数
                translateAnimation.setDuration(300);
                //动画的反复次数
                translateAnimation.setRepeatCount(0);
                //设置为true，动画转化结束后被应用
                translateAnimation.setFillAfter(true);
                subview.startAnimation(translateAnimation);
                break;
            }
            default:{
                break;
            }
        }



    }

    /**
     * 从subview关联的superview上,移除subview,并触发回调.
     * @param subview
     */
     public void undisplay(final View subview, final JsViewUndisplayCallback callback) {
        ViewGroup parentView = (ViewGroup)subview.getParent();
         JsViewDisplayStyle style = map_style.get(subview);

         //先动画,再调用回调.
         switch (style.undisplayAnimationType) {
             case None: {
                 undisplayAfterAnimation(subview,callback);
                 break;
             }
             case ToBottom:{
                 final Animation translateAnimation = new TranslateAnimation(0,0,0,0,0,0, Animation.RELATIVE_TO_SELF,1);
                 //动画时间,毫秒数
                 translateAnimation.setDuration(300);
                 //动画的反复次数
                 translateAnimation.setRepeatCount(0);
                 //设置为true，动画转化结束后被应用
                 translateAnimation.setFillAfter(true);
                 subview.startAnimation(translateAnimation);

                 translateAnimation.setAnimationListener(new Animation.AnimationListener() {
                     @Override
                     public void onAnimationStart(Animation animation) {

                     }

                     @Override
                     public void onAnimationEnd(Animation animation) {
                         undisplayAfterAnimation(subview,callback);
                     }

                     @Override
                     public void onAnimationRepeat(Animation animation) {

                     }
                 });
             }
         }
    }

    public void undisplayAfterAnimation(View subview,JsViewUndisplayCallback callback ) {
        ViewGroup parentView = (ViewGroup)subview.getParent();
        parentView.removeView(subview);
        if(callback != null) {
            callback.onUndisplayFinished(subview);
        }
    }
}
